package com.baichang.taoshunda.controller.mobile.delivery;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.DeliveryClerkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(ControllerConstants.DELIVERY)
public class DeliveryMController extends BaseMobileController {

    @Autowired
    private DeliveryClerkService deliveryClerkService;

    /**
     * 手机号登录
     *
     * @param request
     * @param response
     */
    @RequestMapping("/mobileMsgLogin")
    public void mobileMsgLogin(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String mobile = getString(json, "mobile", "");
                String code = getString(json, "code", "");
                return deliveryClerkService.mobileMsgLogin(mobile, code);
            }
        });
    }

    /**
     * 账号登录
     *
     * @param request
     * @param response
     */
    @RequestMapping("/accountLogin")
    public void accountLogin(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String account = getString(json, "account", "");
                String pwd = getString(json, "pwd", "");
                return deliveryClerkService.accountLogin(account, pwd);
            }
        });
    }

    /**
     * 注册
     *
     * @param request
     * @param response
     */
    @RequestMapping("/register")
    public void register(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String account = getString(json, "account", "");
                String code = getString(json, "code", "");
                String pwd = getString(json, "pwd", "");
                String name = getString(json, "name", "");
                String area = getString(json, "area", "");
                String address = getString(json, "address", "");
                Integer cityId = getInt(json, "cityId", 0);
//                Integer areaId = getInt(json, "areaId", 0);
//                Integer provinceId = getInt(json, "provinceId", 0);
                String identityCardNum = getString(json, "identityCardNum", "");
                String handheldPhotos = getString(json, "handheldPhotos", "");
                String fontCard = getString(json, "fontCard", "");
                String reverseCrad = getString(json, "reverseCrad", "");
                return deliveryClerkService.register(account, code, pwd, name, area, address, cityId, identityCardNum, handheldPhotos, fontCard, reverseCrad);
            }
        });
    }

    /**
     * 忘记密码修改密码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateForgetPwd")
    public void updateForgetPwd(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String account = getString(json, "account", "");
                String newPwd = getString(json, "newPwd", "");
                return deliveryClerkService.updateForgetPwd(account, newPwd);
            }
        });
    }

    /**
     * 检查密码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/checkPwd")
    public void checkPwd(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer deliveryClerkId = getInt(json, "deliveryClerkId", 0);
                String pwd = getString(json, "pwd", "");
                return deliveryClerkService.checkPwd(deliveryClerkId, pwd);
            }
        });
    }

    /**
     * 修改密码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updatePwd")
    public void updatePwd(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer deliveryClerkId = getInt(json, "deliveryClerkId", 0);
                String pwd = getString(json, "pwd", "");
                String surePwd = getString(json, "surePwd", "");
                return deliveryClerkService.updatePwd(deliveryClerkId, pwd, surePwd);
            }
        });
    }

    /**
     * 获取配送员信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getDeliveryInfoById")
    public void getDeliveryInfoById(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer deliveryClerkId = getInt(json, "deliveryClerkId", 0);
                return deliveryClerkService.getDeliveryInfoById(deliveryClerkId);
            }
        });
    }


    /**
     * 修改配送员信息
     * 实名认证的名字不能修改
     *
     * @return
     */
    @RequestMapping("/updateInfoById")
    public void updateInfoById(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer deliveryClerkId = getInt(json, "deliveryClerkId", 0);
                String name = getString(json, "name", "");
                String headPic = getString(json, "icon", "");
                return deliveryClerkService.updateInfoById(deliveryClerkId, headPic, name);
            }
        });
    }

    /**
     * 变更手机号
     * 1.旧手机号获取验证码
     * 2.旧手机号验证验证码
     * 3.新手机号获取验证码
     * 4.新手机号验证验证码
     * 5.修改手机号
     *
     * @return
     */
    @RequestMapping("/changePhone")
    public void changePhone(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer deliveryClerkId = getInt(json, "deliveryClerkId", 0);
                String phone = getString(json, "newPhone", "");
                String code = getString(json, "code", "");
                return deliveryClerkService.changePhone(deliveryClerkId, code, phone);
            }
        });
    }

    /**
     * 实名认证
     *
     * @param request
     * @param response
     */
    @RequestMapping("/realNameAuth")
    public void realNameAuth(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String name = getString(json, "name", "");
                Integer deliveryClerkId = getInt(json, "deliveryClerkId", 0);
                String identityCardNum = getString(json, "identityCardNum", "");
                String fontCard = getString(json, "fontCard", "");
                String reverseCrad = getString(json, "reverseCrad", "");
                String handheldPhotos = getString(json, "handheldPhotos", "");
                return deliveryClerkService.realNameAuth(name, deliveryClerkId, identityCardNum, fontCard, reverseCrad, handheldPhotos);
            }
        });
    }

    /**
     * 上传经纬度
     *
     * @param request
     * @param response
     */
    @RequestMapping("/uploadLatLng")
    public void uploadLatLng(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer deliveryId = getInt(json, "deliveryId", 0);
                Integer cityId = getInt(json, "cityId", 0);
                String lat = getString(json, "lat", "");
                String lng = getString(json, "lng", "");
                return deliveryClerkService.uploadLatLng(deliveryId, cityId, lat, lng);
            }
        });
    }

    /**
     * 获取实名认证信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getRealNameAuthInfo")
    public void getRealNameAuthInfo(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer id = getInt(json, "id", 0);
                return deliveryClerkService.getRealNameAuthInfo(id);
            }
        });
    }

    /**
     * 开始接单
     *
     * @param request
     * @param response
     */
    @RequestMapping("/open")
    public void open(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer deliveryId = getInt(json, "deliveryId", 0);
                return deliveryClerkService.updateIsOpen(deliveryId, 1);
            }
        });
    }

    /**
     * 停止接单
     *
     * @param request
     * @param response
     */
    @RequestMapping("/close")
    public void close(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer deliveryId = getInt(json, "deliveryId", 0);
                return deliveryClerkService.updateIsOpen(deliveryId, 2);
            }
        });
    }

    /**
     * 查询缴纳保障金的状态以及保障金的金额
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getKetubbah")
    public void getKetubbah(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer deliveryId = getInt(json, "deliveryId", 0);
                return deliveryClerkService.getKetubbah(deliveryId);
            }
        });
    }

    /**
     * 查询缴纳保障金的状态以及保障金的金额
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getKetubbahList")
    public void getKetubbahList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer deliveryId = getInt(json, "deliveryId", 0);
                Integer nowPage = getInt(json, "nowPage", 1);
                Integer pageSize = getInt(json, "pageSize", 20);
                return deliveryClerkService.getKetubbahList(deliveryId,nowPage,pageSize);
            }
        });
    }

    /**
     * 缴纳保障金
     *
     * @param request
     * @param response
     */
    @RequestMapping("/payBail")
    public void payBail(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer deliveryId = getInt(json, "deliveryId", 0);
                double payBail = getDouble(json, "payBail", 0);
                double payType = getDouble(json, "payType", 1);
                return deliveryClerkService.payBail(deliveryId,payBail,payType);
            }
        });
    }

    /**
     * 查询配送员必看
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findGuide")
    public void findGuide(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {

                return deliveryClerkService.findGuide();
            }
        });
    }


}
